﻿using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Matrix.Common.DBUtility;
using Matrix.MaKaYunShang.Model;

namespace Matrix.MaKaYunShang.DAL
{
	public partial class ConsumerAddressDAL : BaseDAL<ConsumerAddressDAL>
	{
        #region 默认方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ConsumerAddress model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into ConsumerAddress(");
            strSql.Append("ConsumerId,AreaId,Address,AddressDetail,LinkMan,LinkMobile,Longitude,Latitude,IsDefault,IsDelete");
            strSql.Append(") values (");
            strSql.Append("@ConsumerId,@AreaId,@Address,@AddressDetail,@LinkMan,@LinkMobile,@Longitude,@Latitude,@IsDefault,@IsDelete");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@ConsumerId", SqlDbType.Int,4) ,            
                        new SqlParameter("@AreaId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Address", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@AddressDetail", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@LinkMan", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@LinkMobile", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Longitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@Latitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@IsDefault", SqlDbType.Bit,1) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };

            parameters[0].Value = model.ConsumerId;
            parameters[1].Value = model.AreaId;
            parameters[2].Value = model.Address;
            parameters[3].Value = model.AddressDetail;
            parameters[4].Value = model.LinkMan;
            parameters[5].Value = model.LinkMobile;
            parameters[6].Value = model.Longitude;
            parameters[7].Value = model.Latitude;
            parameters[8].Value = model.IsDefault;
            parameters[9].Value = model.IsDelete; var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            return obj == null ? 0 : Convert.ToInt32(obj);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ConsumerAddress model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update ConsumerAddress set ");

            strSql.Append(" ConsumerId = @ConsumerId , ");
            strSql.Append(" AreaId = @AreaId , ");
            strSql.Append(" Address = @Address , ");
            strSql.Append(" AddressDetail = @AddressDetail , ");
            strSql.Append(" LinkMan = @LinkMan , ");
            strSql.Append(" LinkMobile = @LinkMobile , ");
            strSql.Append(" Longitude = @Longitude , ");
            strSql.Append(" Latitude = @Latitude , ");
            strSql.Append(" IsDefault = @IsDefault , ");
            strSql.Append(" IsDelete = @IsDelete  ");
            strSql.Append(" where Id=@Id ");

            SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@ConsumerId", SqlDbType.Int,4) ,            
                        new SqlParameter("@AreaId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Address", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@AddressDetail", SqlDbType.NVarChar,100) ,            
                        new SqlParameter("@LinkMan", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@LinkMobile", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@Longitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@Latitude", SqlDbType.NVarChar,16) ,            
                        new SqlParameter("@IsDefault", SqlDbType.Bit,1) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.ConsumerId;
            parameters[2].Value = model.AreaId;
            parameters[3].Value = model.Address;
            parameters[4].Value = model.AddressDetail;
            parameters[5].Value = model.LinkMan;
            parameters[6].Value = model.LinkMobile;
            parameters[7].Value = model.Longitude;
            parameters[8].Value = model.Latitude;
            parameters[9].Value = model.IsDefault;
            parameters[10].Value = model.IsDelete; return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("update ConsumerAddress set IsDelete=1 ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            var strSql = new StringBuilder();
            strSql.Append("update ConsumerAddress set IsDelete=1 ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataSet GetModel(int Id)
        {
            var strSql = new StringBuilder();
            strSql.Append("select Id, ConsumerId, AreaId, Address, AddressDetail, LinkMan, LinkMobile, Longitude, Latitude, IsDefault, IsDelete  ");
            strSql.Append("  from ConsumerAddress ");
            strSql.Append(" where Id=@Id  and IsDelete=0");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds.Tables[0].Rows.Count > 0 ? ds : null;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM ConsumerAddress where IsDelete=0 ");
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" and " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion
		
		#region 扩展方法

        /// <summary>
        /// 删除默认地址
        /// </summary>
        public bool DeleteDefaultAddress(int consumerId)
        {
            var strSql = new StringBuilder();
            strSql.Append("update ConsumerAddress set IsDefault=0 ");
            strSql.Append(" where ConsumerId=@ConsumerId");
            SqlParameter[] parameters = {
					new SqlParameter("@ConsumerId", SqlDbType.Int,4)
			};
            parameters[0].Value = consumerId;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
        }
        /// <summary>
        /// 通过用户ID获取地址列表
        /// </summary>
        public List<ConsumerAddress> GetModelListByConsumerId(int consumerId)
        {
            var sbSql = new StringBuilder();
            sbSql.Append("select * from ConsumerAddress ");
            sbSql.Append(" where ConsumerId=@ConsumerId  and IsDelete=0");
            SqlParameter[] parameters = {
					new SqlParameter("@ConsumerId", SqlDbType.Int,4)
			};
            parameters[0].Value = consumerId;

            var lsAddress = DbHelperSQL.ExecuteSqlList<ConsumerAddress>(sbSql.ToString(), parameters);
            if (lsAddress != null && lsAddress.Count > 0)
            {
                return lsAddress.ToList();
            }
            return null;
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <returns></returns>
        public List<ConsumerAddress> GetPagingList(int pageIndex, int pageSize, string searchKeyWord, string colOrder, ref int totalCount, int consumerId)
        {
            string strWhere = " IsDelete = 0 ";
            if (consumerId > 0)
            {
                strWhere += " and consumerId=" + consumerId;
            }
            return GetSimpleListPage<ConsumerAddress>(pageIndex, pageSize, "ConsumerAddress", null, " * ,(case when IsDefault=1 then '是' else '否' end)as IsDefaultStr ", colOrder, strWhere, ref totalCount);
        }
		#endregion
	}
}
